const nodemailer = require("nodemailer"); // 发送邮件依赖模块

const emailConfig = require('config/email-config'); // 邮箱配置

const smtp = emailConfig.smtp;
const mailFrom = emailConfig.mailFrom;
const mailPwd = emailConfig.mailPwd;

const service = {};

service.send = (email, subject, html, callback) => {
  let transporter = nodemailer.createTransport({
    host: smtp,
    auth: {
      user: mailFrom,
      pass: mailPwd // 授权码,通过QQ获取
    }
  });
  let mailOptions = {
    from: mailFrom, // 发送者
    to: email, // 接受者,可以同时发送多个,以逗号隔开
    subject: subject, // 标题
  };
  if (html !== null) {
    mailOptions.html = html; // html
  }
  transporter.sendMail(mailOptions, function(err, info) {
    callback(err);
  });
}

module.exports = service;